﻿using MediatR;
using Medical.Api.Read.Application.Command.DepartmentManagement.Adopted;
using Medical.Api.Read.Dto.DepartmentManagement.Adopted;
using Medical.Domain.DepartmentInfo;
using Medical.ErrorCode;
using Medical.Interstructrue.Base;
using Microsoft.Extensions.Logging;

namespace Medical.Api.Read.Application.Handler.DepartmentManagement.Adopted
{
    public class GetAdoptedGetModelCommandHandler : IRequestHandler<GetAdoptedGetModelCommands, ApiResult<AdoptedDetailDto>>
    {
        private readonly IBaseRepository<TouseTable> touseRep;
        private readonly IBaseRepository<ItemTable> itemRep;
        private readonly ILogger<GetAdoptedGetModelCommandHandler> logger;

        public GetAdoptedGetModelCommandHandler(IBaseRepository<TouseTable> touseRep, IBaseRepository<ItemTable> itemRep, ILogger<GetAdoptedGetModelCommandHandler> logger)
        {
            this.touseRep = touseRep;
            this.itemRep = itemRep;
            this.logger = logger;
        }

        public async Task<ApiResult<AdoptedDetailDto>> Handle(GetAdoptedGetModelCommands request, CancellationToken cancellationToken)
        {
            try
            {
                logger.LogInformation($"开始反填领用申请，领用申请ID：{request.touseId}");
                
                // 根据touseId查询领用申请表
                var touseTable = touseRep.GetModel(request.touseId);
                
                if (touseTable == null)
                {
                    logger.LogWarning($"未找到ID为{request.touseId}的领用申请记录");
                    return new ApiResult<AdoptedDetailDto>
                    {
                        Code = ApiEnums.Error,
                        Data = null,
                        Msg = $"未找到ID为{request.touseId}的领用申请记录"
                    };
                }
                
                // 根据领用申请的ItemId查询物品表
                var itemTable = itemRep.GetAll().Where(i => i.Id == touseTable.ItemId).FirstOrDefault();
                
                if (itemTable == null)
                {
                    logger.LogWarning($"未找到领用申请关联的物品记录，ItemId：{touseTable.ItemId}");
                }

                // 创建包含领用申请和物品信息的DTO
                var adoptedDetailDto = new AdoptedDetailDto
                {
                    TouseInfo = touseTable,
                    ItemInfo = itemTable
                };

                logger.LogInformation($"成功获取领用申请数据，ID：{request.touseId}");
                
                return new ApiResult<AdoptedDetailDto>
                {
                    Code = ApiEnums.Success,
                    Data = adoptedDetailDto,
                    Msg = "领用申请数据获取成功"
                };
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"反填领用申请时发生异常：{ex.Message}");
                
                return new ApiResult<AdoptedDetailDto>
                {
                    Code = ApiEnums.Error,
                    Data = null,
                    Msg = $"反填领用申请失败：{ex.Message}"
                };
            }
        }
    }
}
